﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmUserManage : Form
    {
        SqlTools sqt;

        string strSelectUsers = "select Uname 用户名称,UserCharacter 用户角色,Unuser 用户停用 from UserList where Mname='TJGL' order by Uname";
        string strCheckUserExist = "select Uname from UserList where Mname='TJGL' and Uname='@用户名称'";
        string strInsertUser = "insert into UserList (Mname,Uname,Unuser,UserCharacter) values ('TJGL','@用户名称',0,'@用户角色')";
        string strUpdateUser = "update UserList set @字段='@值' where Mname='TJGL' and Uname='@用户名称'";
        string strDeleteUser = "delete from UserList where Mname='TJGL' and Uname='@用户名称'";

        public frmUserManage()
        {
            InitializeComponent();
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                cmbCharacter_Invisible.Top = dgv1.GetCellDisplayRectangle(1, dgv1.CurrentRow.Index, true).Top + dgv1.Top;
                cmbCharacter_Invisible.Left = dgv1.GetCellDisplayRectangle(1, dgv1.CurrentRow.Index, true).Left + dgv1.Left;
                //cmbCharacter_Invisible.Height = dgv1.GetCellDisplayRectangle(1, dgv1.CurrentRow.Index, true).Height;
                btnSave_Invisible.Top = cmbCharacter_Invisible.Top;
                btnSave_Invisible.Left = cmbCharacter_Invisible.Left + cmbCharacter_Invisible.Width;
                //btnSave_Invisible.Height = cmbCharacter_Invisible.Height;
                cmbCharacter_Invisible.Visible = true;
                btnSave_Invisible.Visible = true;
                btnStopUse_Invisible.Top = dgv1.GetCellDisplayRectangle(2, dgv1.CurrentRow.Index, true).Top+dgv1.Top ;
                btnStopUse_Invisible.Left = dgv1.GetCellDisplayRectangle(2, dgv1.CurrentRow.Index, true).Left + dgv1.Left;
                btnStopUse_Invisible.Size = dgv1.GetCellDisplayRectangle(2, dgv1.CurrentRow.Index, true).Size;
                cmbCharacter_Invisible.SelectedIndex = cmbCharacter_Invisible.Items.IndexOf(dgv1.CurrentRow.Cells["用户角色"].Value);
                if (GeneralTools.GetInt(dgv1.CurrentRow.Cells["用户停用"].Value, 0) == 0)
                {
                    btnStopUse_Invisible.Text = "停  用";
                }
                else
                {
                    btnStopUse_Invisible.Text = "启  用";
                }
                btnStopUse_Invisible.Visible = true;
            }
            else
            {
                cmbCharacter_Invisible.Visible = false ;
                btnSave_Invisible.Visible = false ;
                btnStopUse_Invisible.Visible = false;
            }
        }

        private void frmUserManage_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            cmbCharacter.SelectedIndex = 0;
            iniDatagridView();
        }

        private void iniDatagridView()
        {
            dgv1.DataSource = sqt.GetData(strSelectUsers);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("用户名称不能为空！");
                txtUserName.Focus();
                return;
            }
            if (sqt.RecordExist(strCheckUserExist.Replace("@用户名称", txtUserName.Text)))
            {
                GeneralTools.ShowInfoMsg("该用户名称已存在！");
                txtUserName.Focus();
                return;
            }
            if (sqt.DoCmdExecuteNonQuery(strInsertUser.Replace("@用户名称", txtUserName.Text)
                                                                                 .Replace("@用户角色", cmbCharacter.Text)) > 0)
            {
                if (frmChangePassword.Show(txtUserName.Text, "设置密码")==DialogResult .Cancel )
                {
                    sqt.DoCmdExecuteNonQuery(strDeleteUser.Replace("@用户名称", txtUserName.Text));
                }
                iniDatagridView();
            }
            else
            {
                GeneralTools.ShowErrorMsg("添加用户失败：" + sqt.ErrMessage);
            }
        }

        private void txtUserName_Leave(object sender, EventArgs e)
        {
            txtUserName.Text = txtUserName.Text.Trim();
        }

        private void btnSave_Invisible_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                string strTmpUserName = GeneralTools.GetString(dgv1.CurrentRow.Cells["用户名称"].Value, string.Empty);
                if (cmbCharacter_Invisible.Text.Equals(string.Empty))
                {
                    GeneralTools.ShowInfoMsg("请先选择要设置的角色！");
                    cmbCharacter_Invisible.Focus();
                    return;
                }
                if (sqt.DoCmdExecuteNonQuery(strUpdateUser.Replace("@用户名称", strTmpUserName)
                                                                                                .Replace("@字段", "UserCharacter")
                                                                                                .Replace("@值", cmbCharacter_Invisible.Text)) > 0)
                {
                    iniDatagridView();
                }
                else
                {
                    GeneralTools.ShowErrorMsg("用户角色更新失败：" + sqt.ErrMessage);
                    return;
                }
            }
        }

        private void btnStopUse_Invisible_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                string strTmpUserName = GeneralTools.GetString(dgv1.CurrentRow.Cells["用户名称"].Value, string.Empty);
                if (btnStopUse_Invisible .Text.Equals("启  用"))
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateUser.Replace("@用户名称", strTmpUserName)
                                                                                                .Replace("@字段", "Unuser")
                                                                                                .Replace("@值", "0"));
                }
                else
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateUser.Replace("@用户名称", strTmpUserName)
                                                                                                .Replace("@字段", "Unuser")
                                                                                                .Replace("@值", "1"));
                }
                iniDatagridView();
            }
        }

        private void tsmiChangePassword_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                string strTmpUserName = GeneralTools.GetString(dgv1.CurrentRow.Cells["用户名称"].Value, string.Empty);
                frmChangePassword newFrmChangePassword = new frmChangePassword(strTmpUserName);
                newFrmChangePassword.ShowDialog();
            }
        }
    }
}